package com.daryl.jcu_project1_creditscoreform;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import com.daryl.jcu_project1_creditscoreform.R.id;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class methods extends Display_form_personal_details {
	static LoginPage lp;
	static int id;
	static String variable;
	static EditText et;
	public static String VexistingCustomer;

	public void postData(Context context, String username, String password) {
		// Create a new HttpClient and Post Header
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(
				"http://chandrajcu.webfactional.com/jcumfidatabase/datatestsearchquery.php");
		// HttpPost httppost = new
		// HttpPost("http://10.0.2.2:1234/datatest.php");

		// This is the data to send
		// String MyName; //any data to send

		try {
			lp = new LoginPage();
			// Add your data
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
			nameValuePairs.add(new BasicNameValuePair("username", username));
			nameValuePairs.add(new BasicNameValuePair("password", password));

			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

			// Execute HTTP Post Request

			ResponseHandler<String> responseHandler = new BasicResponseHandler();
			String response = httpclient.execute(httppost, responseHandler);

			// This is the response from a php application
			// String reverseString = response;

			String[] splits = response.split(":");

			System.out.println("test 2 " + splits[0]);

			System.out.println(" 1 " + response);

			// toast 1 version
			// Toast msg = Toast.makeText(context, "response " + reverseString,
			// Toast.LENGTH_LONG);

			// toast 2 version
			Toast msg = Toast.makeText(context, "response " + splits[0],
					Toast.LENGTH_LONG);
			System.out.println("Response " + splits[0]);
			System.out.println("Response234 " + response);

			// System.out.println(" 2 ");
			msg.show();
			// System.out.println(" 3 " + reverseString);

			variables vb = new variables();
			vb.setUserLoginName();

		} catch (ClientProtocolException e) {

			Toast.makeText(context, "CPE response " + e.toString(),
					Toast.LENGTH_LONG).show();
			// TODO Auto-generated catch block
		} catch (IOException e) {
			Toast.makeText(context, "IOE response" + e.toString(),
					Toast.LENGTH_LONG).show();
			System.out.println("IOE response" + e.toString());
			// TODO Auto-generated catch block
		}

	}// end postData()

	// get customer title
	public void getEditTextvalue(EditText et, int id, String variable) {
		variables vb = new variables();
		et = (EditText) findViewById(id);
		vb.variable = et.getText().toString();
	}

	public void editorSharePreferences(Context ctx, String dataName,
			String valueToAddIn) {
		SharedPreferences preferences = ctx.getSharedPreferences("Mb4Me", 0);
		SharedPreferences.Editor editor = preferences.edit();

		editor.putString(dataName, valueToAddIn);
		editor.commit();

	}

	public void editorShareIntPreferences(Context ctx, String dataName,
			int valueToAddIn) {
		SharedPreferences preferences = ctx.getSharedPreferences("Mb4Me", 0);
		SharedPreferences.Editor editor = preferences.edit();

		editor.putInt(dataName, valueToAddIn);
		editor.commit();

	}

	public String retriveSharePreference(Context ctx, String retriveValue,
			String dataName) {
		SharedPreferences preferences = ctx.getSharedPreferences("Mb4Me", 0);
		retriveValue = preferences.getString(dataName, null);
		return retriveValue;
	}

	public Integer retriveShareIntPreference(Context ctx, int retriveValue,
			String dataName) {
		SharedPreferences preferences = ctx.getSharedPreferences("Mb4Me", 0);
		retriveValue = preferences.getInt(dataName, retriveValue);
		return retriveValue;
	}

	public String setExistingCustomer() {
		return VexistingCustomer;
	}

	// get getTextValue
	public void getEditTextValue(EditText edittext, int resources, String  DataValue , String vEditTextValue) {

		methods md = new methods();
		System.out.println("Resources1 " + resources);
		//System.out.println("edittext1 " + editTextName);
		edittext = (EditText) findViewById(resources);
		vEditTextValue = edittext.getText().toString();
		md.editorSharePreferences(this, DataValue, vEditTextValue);
	}

	// set setTextValue
	public void setEditTextValue(EditText edittext, int resources, String DataValue) {

		methods md = new methods();
		System.out.println("Resources2 " + resources);
		edittext  = (EditText) findViewById(resources);
		edittext.setText(md.retriveSharePreference(this, "vEditTextValue",
				DataValue));

	}

}
